package p.ithorns.framework.redis.stream;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.hash.HashMapper;
import p.ithorns.framework.redis.stream.serializer.StreamKeySerializer;
import p.ithorns.framework.redis.stream.serializer.StreamValueSerializer;

/**
 * RedisStreamTemplate
 * 自定义RedisSteamTemplate
 *
 * @author Ht.L
 * @date 2023-07-06 09:26
 * @since 1.0.0
 */
public class RedisStreamTemplate<T> extends RedisTemplate<String, T> {

    private final HashMapper<Object, String, T> hashMapper;

    private final StreamOperations<String, String, T> streamOps;


    public RedisStreamTemplate(RedisConnectionFactory connectionFactory,
                               StreamKeySerializer keySerializer,
                               StreamValueSerializer<Object> valueSerializer,
                               HashMapper<Object, String, T> hashMapper) {
        setKeySerializer(keySerializer);
        setHashKeySerializer(keySerializer);
        setValueSerializer(valueSerializer);
        setHashValueSerializer(valueSerializer);

        this.hashMapper = hashMapper;
        this.streamOps = this.opsForStream(hashMapper);

        setConnectionFactory(connectionFactory);
        afterPropertiesSet();
    }

    public HashMapper<Object, String, T> getHashMapper() {
        return hashMapper;
    }

    public StreamOperations<String, String, T> getStreamOps() {
        return streamOps;
    }

}